package com.jiumi.baseconfig.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jiumi.common.annotation.Log;
import com.jiumi.common.core.controller.BaseController;
import com.jiumi.common.core.domain.AjaxResult;
import com.jiumi.common.enums.BusinessType;
import com.jiumi.baseconfig.domain.BaseMealCourse;
import com.jiumi.baseconfig.service.IBaseMealCourseService;
import com.jiumi.common.utils.poi.ExcelUtil;
import com.jiumi.common.core.page.TableDataInfo;

/**
 * 套餐课程Controller
 *
 * @author jiumi
 * @date 2023-05-21
 */
@RestController
@RequestMapping("/baseconfig/mealcourse")
public class BaseMealCourseController extends BaseController
{
    @Autowired
    private IBaseMealCourseService baseMealCourseService;

    /**
     * 查询套餐课程列表
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:mealcourse:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseMealCourse baseMealCourse)
    {
        startPage();
        List<BaseMealCourse> list = baseMealCourseService.selectBaseMealCourseList(baseMealCourse);
        return getDataTable(list);
    }

    /**
     * 导出套餐课程列表
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:mealcourse:export')")
    @Log(title = "套餐课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseMealCourse baseMealCourse)
    {
        List<BaseMealCourse> list = baseMealCourseService.selectBaseMealCourseList(baseMealCourse);
        ExcelUtil<BaseMealCourse> util = new ExcelUtil<BaseMealCourse>(BaseMealCourse.class);
        util.exportExcel(response, list, "套餐课程数据");
    }

    /**
     * 获取套餐课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:mealcourse:query')")
    @GetMapping(value = "/{mealId}")
    public AjaxResult getInfo(@PathVariable("mealId") Long mealId)
    {
        return success(baseMealCourseService.selectBaseMealCourseByMealId(mealId));
    }

    /**
     * 新增套餐课程
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:mealcourse:add')")
    @Log(title = "套餐课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseMealCourse baseMealCourse)
    {
        return toAjax(baseMealCourseService.insertBaseMealCourse(baseMealCourse));
    }
    /**
     * 新增套餐课程
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:mealcourse:add')")
    @Log(title = "套餐课程", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddMealCourse")
    public AjaxResult batchAddMealCourse(@RequestBody BaseMealCourse baseMealCourse)
    {
        Long[] courseIds = baseMealCourse.getCourseIds();
        if(courseIds.length == 0){
            return AjaxResult.error("请选择添加的课程");
        }
        int i = 0;
        for (Long courseId : courseIds) {
            BaseMealCourse course = new BaseMealCourse();
            course.setMealId(baseMealCourse.getMealId());
            course.setCourseId(courseId);
            i+=baseMealCourseService.insertBaseMealCourse(course);
        }
        return toAjax(i);
    }

    /**
     * 修改套餐课程
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:mealcourse:edit')")
    @Log(title = "套餐课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseMealCourse baseMealCourse)
    {
        return toAjax(baseMealCourseService.updateBaseMealCourse(baseMealCourse));
    }

    /**
     * 删除套餐课程
     */
    @PreAuthorize("@ss.hasPermi('baseconfig:mealcourse:remove')")
    @Log(title = "套餐课程", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody BaseMealCourse baseMealCourse)
    {
        return toAjax(baseMealCourseService.deleteBaseMealCourseByMealIds(baseMealCourse));
    }
}
